<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class OrderController extends Controller {
	
	/**
	 * 魔术方法，设置默认处理机制
	 * @param String $method
	 * @param String $args
	 */
	public function __call($method, $args){
		if (method_exists('OrderController', $method)){
			$this->$method;
		}else{
			$response['STATUS'] = "error";
			$response['ERRMSG'] = "no method";
			$response['RESULT'] = array();
			$this->ajaxReturn($response);
		}
	}

    public function createOrder(){

		$this->assign('WEB_ROOT',C('WEB_ROOT'));
		// $user=cookie('jspz');
		// if(empty($user)){
		// 	$this->redirect('User/toLogin');
		// }
		// $u=M('users')->where(array("phone"=>$user))->find();
		// if(empty($u)){
		// 	cookie('jspz',null);
		// 	$this->redirect('User/toLogin');
		// }
		$order['product_id'] = I('param.pid');
		$order['service_type']=I('param.service_type');
		$order['service_way']=I('param.service_way');
		$order['area']=I('param.area');

		if(empty($order['area'])){
			$order['area']="全国";
		}

		$oldorder = session("order");
		if(empty($oldorder)){
			if(empty($order['product_id']) || 
			   empty($order['service_type']) || 
			   empty($order['service_way'])){
				$this->error('请选择有效信息');return;
			}
		}
		$order['create_at']=date("Y-m-d h:i:s");
		$order['status'] = 1;
		$order['ordcode'] = getordcode();
		// $res=M('order')->add();
		// if(empty($res)){
		// 	$this->error('创建失败');
		// }
		//$order['id']=$res;
		$product=M("product")->where(array("id"=>$order['product_id']))->find();
		$pid=$product['id'];
		$order['product_name'] = $product['name'];
		$order['product_price']=$product['pre_price'];
		$order['update_at']=date("Y-m-d h:i:s");
		$model = new Model();

		cookie("temp_pid",$order['product_id']);
		cookie("temp_sway",$order['service_way']);
		cookie("temp_area",$order['area']);
		cookie("temp_type",$order['service_type']);
		//获取服务这列表
		// $waiters = $model->field('w.id,w.name,w.firm,
		// 	w.area,w.pic,w.work_age,w.title,w.price')->table('waiter as w, waiter_product as p')
		// 	->where("p.product_id=$pid and p.waiter_id=w.id")
		// 	->order('w.create_at')->select(); 
		$temp_pid=$order['product_id'];
		$temp_sway=$order['service_way'];
		$temp_area=$order['area'];
		$firm_prices=array();
		//别获取不同机构的地区价格，精确匹配找到价格
		//逐个查取
		$firm1_price=$model->field('firm_id,min(price) as price')->table('sku')
			  ->where("product_id=$temp_pid and type=$temp_sway and firm_id = 1 and area like '%$temp_area%' ")
			  ->group('firm_id')->order('firm_id')->select();
	    if(empty($firm1_price)){
	    	$firm1_price=$model->field('firm_id,min(price) as price')->table('sku')
			  ->where("product_id=$temp_pid and type=$temp_sway and firm_id = 1 and area like '%其他%' ")
			  ->group('firm_id')->order('firm_id')->select();
	    }
	    //2
	    $firm2_price=$model->field('firm_id,min(price) as price')->table('sku')
			  ->where("product_id=$temp_pid and type=$temp_sway and firm_id = 2 and area like '%$temp_area%' ")
			  ->group('firm_id')->order('firm_id')->select();
	    if(empty($firm2_price)){
	    	$firm2_price=$model->field('firm_id,min(price) as price')->table('sku')
			  ->where("product_id=$temp_pid and type=$temp_sway and firm_id = 2 and area like '%其他%' ")
			  ->group('firm_id')->order('firm_id')->select();
	    }
	    //3
	    $firm3_price=$model->field('firm_id,min(price) as price')->table('sku')
			  ->where("product_id=$temp_pid and type=$temp_sway and firm_id = 3 and area like '%$temp_area%' ")
			  ->group('firm_id')->order('firm_id')->select();
	    if(empty($firm3_price)){
	    	$firm3_price=$model->field('firm_id,min(price) as price')->table('sku')
			  ->where("product_id=$temp_pid and type=$temp_sway and firm_id = 3 and area like '%其他%' ")
			  ->group('firm_id')->order('firm_id')->select();
	    }
	   

	    if(!empty($firm1_price)){
	    	$firm_prices[0]["firm_id"]=$firm1_price[0]["firm_id"];
	    	$firm_prices[0]["price"]=$firm1_price[0]["price"];
	    }
	    if(!empty($firm2_price)){
	    	$firm_prices[1]["firm_id"]=$firm2_price[0]["firm_id"];
	    	$firm_prices[1]["price"]=$firm2_price[0]["price"];
	    }
	    if(!empty($firm3_price)){
	    	$firm_prices[2]["firm_id"]=$firm3_price[0]["firm_id"];
	    	$firm_prices[2]["price"]=$firm3_price[0]["price"];
	    }
	    
		// if($temp_area == "全国"){
		// 	$waiters = $model->field('w.id,w.name,w.firm,
		// 	w.area,w.pic,w.work_age,w.title,w.firm_id,w.order_num')->table('waiter as w,service_area as s')
		// 	->where("w.id=s.wid")
		// 	->group("w.id")->order('w.create_at')->select(); 
		// }else{
		// 	$waiters = $model->field('w.id,w.name,w.firm,
		// 	w.area,w.pic,w.work_age,w.title,w.firm_id,w.order_num')->table('waiter as w,service_area as s')
		// 	->where("(s.area like '%$temp_area%' or s.area like '%全国%') and w.id=s.wid")
		// 	->group("w.id")->order('w.create_at')->select(); 
		// }
	    //dump($firm_price);die();
	    
		// $firm_price=$model->field('firm_id,min(price) as price')->table('sku')
		// 	  ->where("product_id=$temp_pid and type=$temp_sway and area like '%$temp_area%' ")
		// 	  ->group('firm_id')->order('firm_id')->select();
	    $waiters = $model->field('w.id,w.name,w.firm,
			w.area,w.pic,w.work_age,w.title,w.firm_id,w.order_num')->table('waiter as w,service_area as s')
			->where("s.area like '%$temp_area%' and w.id=s.wid")
			->group("w.id")->order('w.create_at')->select(); 
		// $this->assign("order",$order);
		session("order",$order);
		session("firm_price",$firm_prices);
		
		$this->assign("waiters",$waiters);
		$this->assign("firm_price",$firm_prices);
		$this->assign("product",$product);
		$this->display("service");
	 }

	 public function makeOrder(){
	 	$this->assign('WEB_ROOT',C('WEB_ROOT'));
	 	$order = session("order");
	 	$wid = I('param.wid');

	 	$user=cookie('jspz');
	 	
		if(empty($user)){
			$this->redirect('User/toLogin');
		}
		$u=M('users')->where(array("phone"=>$user))->find();
		if(empty($u)){
			cookie('jspz',null);
			$this->redirect('User/toLogin');
		}
	 	if(empty($order)){
	 		$this->error("时间超时,请重新选择服务",U('Index/index'));
	 	}
	 	$waiter=M('waiter')->where(array("id"=>$wid))->find();
	 	if(empty($waiter)){
	 		$this->error("服务人员已下班，请刷新页面");
	 	}
	 	$firm_price=session("firm_price");
	 	$order['user_id']=$u['id'];
	 	$order['waiter_name']=$waiter['name'];
	 	$order['update_at']=date("Y-m-d h:i:s");
		$order['status'] = 2;
		$order['waiter_id'] = $waiter['id'];
		$order['waiter_price']=$waiter['price'];
		$order['total_price']=$firm_price[$waiter['firm_id']-1]['price'];
	 	$test = session("order");
	 	if(empty($test['id'])){
	 		$res=M('order')->add($order);
	 	}else{
	 		$res=M('order')->save($order);
	 	}
	 	$product=M('product')->where(array("id"=>$order['product_id']))->find();
		if(empty($res)){
			$this->error('创建失败');
		}
		$order['id']=$res;
		//session("order",null);
		$this->assign("order",$order);
		$this->assign("product",$product);
		session("order",$order);
		
	 	$this->display("checkout");
	 }

	 public function paySuccess(){
	 	$this->assign('WEB_ROOT',C('WEB_ROOT'));
	 	$order=session("order");
	 	$this->assign("order",$order);
	 	$this->display("success");
	 }
	 public function payError(){
	 	$this->assign('WEB_ROOT',C('WEB_ROOT'));
	 	$order=session("order");
	 	$this->assign("order",$order);
	 	$this->display("error");
	 }
    //public function blog(){
    //    $this->assign('WEB_ROOT',C('WEB_ROOT'));
    //    $this->display("blog");
    //}
    //public function singleblog(){
    //    $this->assign('WEB_ROOT',C('WEB_ROOT'));
    //    $this->display("single-blog");
    //}
}
